Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

rss[.]xml.ts

34 lines1.3 KB

1import { createFileRoute } from '@tanstack/solid-router'
2import { allBlogs } from 'content-collections'
3import { SITE_DESCRIPTION, SITE_TITLE, SITE_URL } from '../lib/site'
4​
5export const Route = createFileRoute('/rss.xml')({
6 server: {
7 handlers: {
8 GET: () => {
9 const posts = Array.from(
10 new Map(
11 [...allBlogs]
12 .sort((a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf())
13 .map((post) => [post.slug, post]),
14 ).values(),
15 )
16​
17 const items = posts
18 .map((post) => {
19 const url = `${SITE_URL}/blog/${post.slug}`
20 return `<item><title><![CDATA[${post.title}]]></title><description><![CDATA[${post.description}]]></description><link>${url}</link><guid>${url}</guid><pubDate>${new Date(post.pubDate).toUTCString()}</pubDate></item>`
21 })
22 .join('')
23​
24 const xml = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[${SITE_TITLE}]]></title><description><![CDATA[${SITE_DESCRIPTION}]]></description><link>${SITE_URL}</link>${items}</channel></rss>`
25​
26 return new Response(xml, {
27 headers: {
28 'Content-Type': 'application/rss+xml; charset=utf-8',
29 },
30 })
31 },
32 },
33 },
34})